package com.gujianjian.rabbitmq.config;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 有过期时间的死信队列
 */
@Component
public class RabbitmqConfig {

    @Bean("queue30s")
    public Queue queue30s(){

        return QueueBuilder.durable("queue30s")
                .withArgument("x-dead-letter-exchange","exchange.dlx1")
                .withArgument("x-dead-letter-routing-key","key.dlx1") // key.dlx.car.rent.#
                .withArgument("x-message-ttl",1000 * 30)
                .build();
    }

    @Bean("exchange1")
    public Exchange exchange1(){
        return ExchangeBuilder.directExchange("exchange1").build();
    }

    @Bean
    public Binding binding1(@Qualifier("queue30s")Queue queue30s,@Qualifier("exchange1")Exchange exchange1){
        return BindingBuilder.bind(queue30s).to(exchange1).with("key.queue30s").noargs();
    }

    @Bean("queueDlx1")
    public Queue queueDlx1(){
        return QueueBuilder.durable("queue.dlx1").build();
    }
    @Bean("exchangeDlx1")
    public Exchange exchangeDlx1(){
        return ExchangeBuilder.directExchange("exchange.dlx1").build();
    }

    @Bean
    public Binding bindingDlx1(@Qualifier("queueDlx1") Queue queueDlx1,@Qualifier("exchangeDlx1") Exchange exchangeDlx1){
        return BindingBuilder.bind(queueDlx1).to(exchangeDlx1).with("key.dlx1").noargs();
    }

}
